﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Xml.Linq;
namespace 玩具商城管理系统.Department
{
    public partial class insert_department : Form
    {
        public insert_department()
        {
            InitializeComponent();
        }
        SqlDataAdapter daDepartment;
        DataSet ds = new DataSet();
        void init()
        {
            DB.GetCn();
            string str = "select * from depart";
            daDepartment = new SqlDataAdapter(str, DB.cn);
            daDepartment.Fill(ds, "department_info");
            DB.cn.Close();
        }
        void showAll()
        {
            DataView dvDepartment = new DataView(ds.Tables["department_info"]);
            dgvDepartment.DataSource = dvDepartment;
            dgvDepartment.Columns[2].Width = 400;
        }
        void dgvstyle()
        {
            this.dgvDepartment.Columns[0].HeaderText = "部门编号";
            this.dgvDepartment.Columns[1].HeaderText = "部门名称";
            this.dgvDepartment.Columns[2].HeaderText = "部门描述";
        }
        private void txtdepartid_TextChanged(object sender, EventArgs e)
        {

        }

        private void insert_department_Load(object sender, EventArgs e)
        {
            init();
            showAll();
            dgvstyle();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (txtdepartid.Text == "" || txtdepartname.Text == "")
            {
                MessageBox.Show("部门编号或部门名称不能为空");
            }
            else
            {
                DB.GetCn();
                DialogResult dr = MessageBox.Show("您确定要修改吗？", "提示", MessageBoxButtons.OKCancel
                    , MessageBoxIcon.Information);
                if (dr == DialogResult.OK)
                {
                    string s_id = "select * from depart where depart_id='" + txtdepartid.Text + "'";
                    DataTable dt1 = DB.GetDataSet(s_id);
                    int a = dgvDepartment.CurrentRow.Index;
                    string str = dgvDepartment.Rows[a].Cells["depart_id"].Value.ToString();
                    DataRow[] ProRows = ds.Tables["department_info"].Select("depart_id='" + str + "'");
                    ProRows[0]["depart_desc"] = txtdepartdesc.Text;
                    ProRows[0]["depart_name"] = txtdepartname.Text;
                    
                        
                }
                else
                {
                    return;
                }
                //使用存储过程添加日志
                SqlCommand cmd = new SqlCommand("add_log", DB.cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("username", SqlDbType.NVarChar));
                cmd.Parameters.Add(new SqlParameter("type", SqlDbType.NVarChar));
                cmd.Parameters.Add(new SqlParameter("action_table", SqlDbType.NVarChar));
                cmd.Parameters.Add(new SqlParameter("action_date", SqlDbType.DateTime));

                cmd.Parameters["username"].Value = login.username;
                cmd.Parameters["type"].Value = "修改";
                cmd.Parameters["action_table"].Value = "depart表";
                cmd.Parameters["action_date"].Value = DateTime.Now;
                try
                {
                    SqlCommandBuilder dbDepartment = new SqlCommandBuilder(daDepartment);
                    daDepartment.Update(ds, "department_info");
                    cmd.ExecuteNonQuery();//执行存储过程
                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void dgvDepartment_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            txtdepartid.Text = dgvDepartment.CurrentRow.Cells["depart_id"].Value.ToString();
            txtdepartname.Text = dgvDepartment.CurrentRow.Cells["depart_name"].Value.ToString();
            txtdepartdesc.Text = dgvDepartment.CurrentRow.Cells["depart_desc"].Value.ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
